package cat.dao;
import java.util.List;
import cat.beans.AdminInfo;
import cat.commons.Enums;
import cat.jdbc.DBUtil;
import cat.utils.PageInfo;
import cat.utils.StrUtil;

public class AdminDao {
	
	//��ӹ���Ա(��ʼ��ӵĹ���Ա,��û�н�ɫ��)
	public int addAdmin(AdminInfo admin){
		String sql="insert into adminInfo (adminName,password,note,state) values (?,?,?,?)";
		Object [] params={
				admin.getAdminName(),
				admin.getPassword(),
				admin.getNote(),
				admin.getState()
		};
		
		return DBUtil.update(sql, params);		
	}
	

	//��ѯ���й���Ա����(����״̬�Ķ���)���ص�Ĭ����long��,����Ҫת��int��)
	//����ѯ����ɾ����ǵ��û�
	public int getAdminCount(){
		String sql="select count(*) from adminInfo where state!=?";
		long count=DBUtil.getScalar(sql,Enums.EmnuAdminState.ɾequest.getValue());
		return new Integer(count+"");
	}
	
	
	//����idɾ������Ա,��������ɾ��,ֻ����һ��ɾ�����
	public int delAdminById(int id){
		String sql="update adminInfo set state=? where id=?";
		return DBUtil.update(sql,Enums.EmnuAdminState.ɾequest.getValue(), id);	
	}

	//����id��ѯ����Ա��Ϣ
	public AdminInfo getAdminById(int id){
		String sql="select * from adminInfo where id=?";
		return DBUtil.getSingleObj(sql, AdminInfo.class, id);
	}
		
	//�����û���������,��֤�û���Ϣ(���ڵ�¼ҳ)
	public AdminInfo getLoginAdmin(String adminName, String password) {
		String sql="select * from adminInfo where adminName=? and password=?  and state !=?";
		return DBUtil.getSingleObj(sql,AdminInfo.class, adminName,password,Enums.EmnuAdminState.ɾequset.getValue());
	}

	//�����û��˺�,��ѯ�û�(�ڱ�ϵͳ��,�˺���Ψ һ��)
	public AdminInfo getAdminByAcc(String adminName) {
		String sql="select * from adminInfo where adminName =? limit 1";  //��limit 1  ��Ϊ�Ż�����,��Ϊ�˺Ų��ظ�
		return DBUtil.getSingleObj(sql, AdminInfo.class, adminName); 	
	}

	//��ҳ��ѯ�û��б�(��ɾ���û�����ѯ)
	//��Ϊ�漰���˽�ɫ,Ӧ�ù�����ѯ���û�������ɫ
	//�����ڽ���������ڽ�����������ʾ�����
	public List<AdminInfo> getAdminList(PageInfo page) {
		String sql="select a.*,b.roleName from adminInfo a left join roleInfo b on a.roleId=b.id  where a.state !=?  order by a.editDate desc limit ?,? "; 
		return DBUtil.getList(sql, AdminInfo.class,Enums.EmnuAdminState.ɾequest.getValue(), page.getBeginRow(),page.getPageSize());
	}

	//ɾ����ѡ�û�
	public int delAdminByIdList(String[] idList) {
		String sql="delete from adminInfo where id in "+StrUtil.linkSql(idList) ; 
		System.out.println(sql);
		
		return  DBUtil.update(sql);
	}

	//�����û�������״̬
	public int setLock(int id ,String lockFlag) {
		String sql="update adminInfo set state=? where id=?";
		return DBUtil.update(sql, lockFlag,id);
	}

	
   //��������
	public int updatePassword(int id, String password) {
		String sql="update adminInfo set password=? where id=?";
		return DBUtil.update(sql, password,id);
	}


	//���¹���Ա��Ϣ
	public int updateAdmin(AdminInfo admin) {
		String sql="update adminInfo set adminName=?, password=?, note=?, state=? where id=? " ;
		Object [] params={
				admin.getAdminName(),
				admin.getPassword(),
				admin.getNote(),
				admin.getState(),
				admin.getId()
		};
		
		return DBUtil.update(sql, params);
	}

	
}
